package com.cloudstreamservice8001.controller;

import com.cloudstreamservice8001.common.Result;
import com.cloudstreamservice8001.param.DeliverInfoCreatOrUpdateParam;
import com.cloudstreamservice8001.param.DeliverInfoDeleteParam;
import com.cloudstreamservice8001.service.DeliveryInfoService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/deliveryInfo")
public class DeliveryInfoController {
    @Resource
    private DeliveryInfoService deliveryInfoService;

    /**
     * 获取配送信息列表
     * 分页查询
     *
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        System.out.println("我是8001服务");
        return Result.success(deliveryInfoService.getDeliveryInfoList(pageNum, pageSize));
    }

    /**
     * 创建/更新配送信息
     * @param deliveryInfo
     * @return
     */
    @PostMapping(value = {"/create", "/update"})
    public Result createOrUpdate(@RequestBody DeliverInfoCreatOrUpdateParam deliveryInfo) {
        if (deliveryInfo.getId() == null) {
            deliveryInfoService.create(deliveryInfo);
            return Result.success("创建成功") ;
        }
        deliveryInfoService.updates(deliveryInfo);
        return Result.success("更新成功");

    }

    /**
     * 删除配送信息
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody DeliverInfoDeleteParam deliverInfoDeleteParam) {
        if(deliverInfoDeleteParam.getId() != null){
            boolean delete = deliveryInfoService.removeById(deliverInfoDeleteParam.getId());
            return delete ? Result.success("删除成功") : Result.error("删除失败");
        }
        if(deliverInfoDeleteParam.getOrderNumber() != null){
            boolean delete = deliveryInfoService.removeByNumber(deliverInfoDeleteParam.getOrderNumber());
            return delete ? Result.success("删除成功") : Result.error("删除失败");
        }
        return Result.error("参数错误");
    }

    /**
     * 根据状态查询配送信息
     */
    @GetMapping("/status")
    public Result listByStatus(@RequestParam("status") Integer status,  @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return Result.success(deliveryInfoService.listByStatus(status,pageNum,pageSize));
    }

    /**
     * 根据订单编号查询配送信息
     */
    @GetMapping("/order")
    public Result getByOrderNumber(@RequestParam("orderNumber") String orderNumber) {
        return Result.success(deliveryInfoService.getByOrderNumber(orderNumber));
    }


}
